package com.youlai.boot.system.controller;

import com.youlai.boot.system.model.entity.Project;
import com.youlai.boot.system.service.ProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ProjectForm;
import com.youlai.boot.system.model.query.ProjectQuery;
import com.youlai.boot.system.model.vo.ProjectVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 项目实践前端控制层
 *
 * @author hypersense
 * @since 2025-03-14 22:49
 */
@Tag(name = "项目实践接口")
@RestController
@RequestMapping("/api/v1/projects")
@RequiredArgsConstructor
public class ProjectController  {

    private final ProjectService projectService;

    @Operation(summary = "项目实践分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:project:query')")
    public PageResult<ProjectVO> getProjectPage(ProjectQuery queryParams ) {
        IPage<ProjectVO> result = projectService.getProjectPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增项目实践")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:project:add')")
    public Result<Void> saveProject(@RequestBody @Valid ProjectForm formData ) {
        boolean result = projectService.saveProject(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取项目实践表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('system:project:edit')")
    public Result<ProjectForm> getProjectForm(
        @Parameter(description = "项目实践ID") @PathVariable Long id
    ) {
        ProjectForm formData = projectService.getProjectFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改项目实践")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:project:edit')")
    public Result<Void> updateProject(
            @Parameter(description = "项目实践ID") @PathVariable Long id,
            @RequestBody @Validated ProjectForm formData
    ) {
        boolean result = projectService.updateProject(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除项目实践")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:project:delete')")
    public Result<Void> deleteProjects(
        @Parameter(description = "项目实践ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = projectService.deleteProjects(ids);
        return Result.judge(result);
    }
}
